import { getUserAddress, getUserOrderList } from 'api/trade'
export default {
  namespaced: true,
  state: {
    addressList: [],
    orderList: []
  },
  mutations: {
    // 设置订单列表数据
    setOrderList (state, playlod) {
      state.orderList = playlod.orderList
    },
    // 设置默认列表
    setAddressList (state, playlod) {
      state.addressList = playlod.addressList
    },
    // 改变默认地址
    changeDefaultAddress (state, playlod) {
      console.log(state.addressList)
      state.addressList.forEach((val) => {
        val.isDefault = '0'
      })
      const { index } = playlod
      console.log(index)
      state.addressList[index].isDefault = '1'
    }
  },
  getters: {
    // 即将结算的商品列表
    detailOrderList (state) {
      return state.orderList?.detailArrayList || []
    },
    // 商品总价格
    totalPrice (state) {
      return state.orderList?.totalAmount || ''
    },
    // 商品总件数
    totalNum (state) {
      return state.orderList?.totalNum || ''
    }
  },
  actions: {
    async onGetUserAddress ({ commit }) {
      const { data: res } = await getUserAddress()
      if (Number(res.code) === 200) {
        commit('setAddressList', {
          addressList: res.data
        })
      }
    },
    async onGetUserOrderList ({ commit }) {
      const { data: res } = await getUserOrderList()
      if (Number(res.code) === 200) {
        const orderList = res.data
        commit('setOrderList', {
          orderList
        })
      }
    }
  }
}
